package com.struts;

import com.struts.annotation.AnnotationParse;
import com.struts.util.FindClass;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ActionServlet
 */

public class ActionServlet extends HttpServlet
{
    private static final long serialVersionUID = 1L;
    
    private Map<String, ActionMapping> configMap = null;
    private List<String> scanList = null;
    
    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config)
        throws ServletException
    {
        String file = config.getInitParameter("config");
        String basePath = config.getServletContext().getRealPath("/");
        scanList = new ArrayList<String>();
        configMap = XmlParse.parse(basePath + file,scanList);
        List<Class> classs = null;
        Map<String, ActionMapping> tem = null;
        for (String classStr : scanList){
            try {
                classs =  FindClass.getClassesByPackageName(classStr);
                tem =AnnotationParse.parse(classs);
                configMap.putAll(tem);
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            } catch (ClassNotFoundException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }
        }

       // Map<String, ActionMapping> map = AnnotationParse.parse()
    }
    
    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        
        String servletPath = request.getServletPath();
        servletPath = headPath(servletPath);
        
        ActionMapping mapping = configMap.get(servletPath);
        ActionForward forward = null;
        if (mapping != null)
        {
            
            ActionForm form = mapping.genFormBeanRequest(request);
            Action action = mapping.newInstanceAction();
            forward = action.execute(mapping, form, request, response);
            
            RequestDispatcher dispatcher = request.getRequestDispatcher(forward.getPath());
            dispatcher.forward(request, response);
        }
    }
    
    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException
    {
        // TODO Auto-generated method stub
        doGet(request, response);
    }
    
    private String headPath(String path)
    {
        return path.substring(0, path.indexOf("."));
    }
    
}
